//
//  TextFieldView.m
//  RandomAccount
//
//  Created by cuibo on 2020/9/15.
//  Copyright © 2020 cuibo. All rights reserved.
//
#define K_MARGIN 15
#define K_PADDING 10
#import "TextFieldView.h"
@interface TextFieldView()
{
    UILabel * _label;
    UITextField * _inputField;
}
@end
@implementation TextFieldView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(K_PADDING,
                                                           0, frame.size.width - 2 * K_PADDING,
                                                           14)];
        _label.font = [UIFont systemFontOfSize:14];
        _label.textColor = [UIColor whiteColor];
        _label.textAlignment = NSTextAlignmentCenter;
        _inputField = [[UITextField alloc] initWithFrame:CGRectMake(K_PADDING, _label.frame.size.height + K_MARGIN, frame.size.width - 2 * K_PADDING, 20)];
        _inputField.placeholder = @"请输入";
        _inputField.borderStyle = UITextBorderStyleNone;
        _inputField.textColor = [UIColor whiteColor];
        _inputField.textAlignment = NSTextAlignmentCenter;
        _inputField.keyboardType = UIKeyboardTypeNumberPad;
        [_inputField addTarget:self action:@selector(inputing:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_label];
        [self addSubview:_inputField];
    }
    return self;
}
- (void)inputing:(UITextField *)field {
    if (self.block) {
        self.block(field.text);
    }
}
- (void)setTitle:(NSString *)title {
    _label.text = title;
}
- (void)setFieldNumber:(int)number {
    _inputField.text = [NSString stringWithFormat:@"%d",number];
}
- (int)getNumber {
    _inputField.text = @(_inputField.text.intValue).stringValue;
    return _inputField.text.intValue;
}
@end
